<?php
namespace Services;

use Helpers\Tool;
use Helpers\Main;
use Helpers\User;
use Helpers\HttpClient;
use Helpers\HttpResponse;
use Helpers\XhException;

class ResBase {

    /**
     * @var \Helpers\HttpClient
     */
    public static $httpClient;
    public static $url;

    /**
     * 初始化配置
     *
     * @throws XhException
     *
     * create by ck 20180712
     */
    private static function init()
    {
        if (!self::$httpClient) {
            $config     = Main::getConfigByName('resServerInterface');
            self::$url  = Tool::getValue($config, 'questionBaseUrl');
            if (!self::$url) {
                throw new XhException('服务url获取失败');
            }
            self::$httpClient = new HttpClient(self::$url);
        }
    }

    /**
     * 根据类型获取字典
     *
     * @param $type
     * @return array
     * @throws XhException
     *
     * create by ck 20180712
     */
    public static function getDictionaries($type)
    {
        self::init();
        static $enumData = [];
        if (!$enumData) {
            $response   = self::$httpClient->get('/api/v1/dictionaries');
            if (!$response->isSuccess()) {
                throw new XhException('字典获取失败');
            }
            $enumData = $response->toArray();
        }
        $targetEnum = [];
        foreach ($enumData as $row) {
            if ($row['paramType'] == $type && $row['status'] == 1) {
                $targetEnum[$row['id']] = $row['name'];
            }
        }
        return $targetEnum;
    }

}